package com.gsww.vue.controller.sys;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gsww.vue.controller.BaseController;
import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysDepartment;
import com.gsww.vue.entity.sys.SysMenu;
import com.gsww.vue.entity.sys.SysRole;
import com.gsww.vue.service.sys.SysRoleService;
import com.gsww.vue.utils.StringHelper;
import com.gsww.vue.utils.TimeHelper;
@RestController
@RequestMapping(value = "/sys")
public class SysRoleController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(SysRoleController.class);
	@Autowired
	private SysRoleService sysRoleService;
//	@Autowired
//	private SysRoleService sysRoleService;
//	@Autowired
//	private DepartmentService departmentService;
	private Map<String,String> resMap=new HashMap<String,String>();

	/**
	 * 获取用户列表
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/roleList",method = RequestMethod.GET)
	public Map<String,Object> roleList(
			HttpServletRequest request,HttpServletResponse response) {
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		String roleName = request.getParameter("roleName");
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			
//			model.addAttribute("userRolemap", userRolemap);
//			model.addAttribute("sortType", orderField);
//			model.addAttribute("orderField", orderField);
//			model.addAttribute("orderSort", orderSort);
//			// 将搜索条件编码成字符串，用于排序，分页的URL
//			model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
//			model.addAttribute("sParams", searchParams);			
			Map<String,Object> data = sysRoleService.getRoleList(roleName, pageNo,pageSize);
			//Map<String,String> userRolemap=new HashMap<String,String>();
			//查询用户的角色
			//map.put("userRolemap", userRolemap);
			map.put("data", data);
			map.put("success", true);
		}catch(Exception ex){
			map.put("success", false);
			ex.printStackTrace();
		}
		return map;
	}
	/**获取权限树Json
	 * @param roleId
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping(value="/roleAuthorizeList",method = RequestMethod.POST)
	public Map<String,Object> roleAuthorizeList(@RequestBody Map<String,Object> params,HttpServletRequest request,HttpServletResponse response)  throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			String treeJson= sysRoleService.getAuthorizeTree(params.get("roleId").toString());
			//System.out.println("---"+treeJson);
			map.put("data", treeJson);
//			response.setCharacterEncoding("UTF-8");
//			response.setHeader("Content-type", "text/html;charset=UTF-8");
//			response.getWriter().write(treeJson);
//			response.flushBuffer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**保存权限
	 * @param roleId
	 * @param nodes
	 * @throws Exception
	 */
	@RequestMapping(value="/roleAuthorizeSave",method = RequestMethod.POST)
	public Map<String,Object> roleAuthorizeSave(@RequestBody Map<String,Object> params,HttpServletRequest request,HttpServletResponse response){
		String roleId = params.get("roleId").toString();
		String keys = params.get("keys").toString();
		//String types = params.get("types").toString();
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			if (roleId == null || keys == null) {
				resMap.put("success",false);
				//response.getWriter().write("error");
			} else {
				sysRoleService.saveAuthorize(roleId, keys.trim());
				//response.getWriter().write("success");
				resMap.put("success",true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("success",false);
		}
		return resMap;
	}
	@RequestMapping(value = "/roleEdit", method = RequestMethod.GET)
	public Map<String,Object> roleEdit(@RequestParam String  roleId, HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			SysRole sysRole = null;
			if(StringHelper.isNotBlack(roleId)){
				sysRole = sysRoleService.findByKey(roleId);
			}else{
				sysRole = new SysRole();
			}
			resMap.put("sysRole", sysRole);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	/**
	 * 保存角色信息
	 */
	//@SuppressWarnings("finally")
	@RequestMapping(value = "/roleSave", method = RequestMethod.POST)
	public Map<String,Object> roleSave(@RequestBody SysRole sysRole,HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			SysRole role = new SysRole();
			//SysUserSession sysUserSession = (SysUserSession) request.getSession().getAttribute("sysUserSession");
			if(StringHelper.isNotBlack(sysRole.getRoleId())){
				role = sysRoleService.findByKey(sysRole.getRoleId());
			}else{
				role.setRoleState("1");
				role.setCreateTime(TimeHelper.getCurrentTime());
			}
			role.setRoleName(sysRole.getRoleName());
			role.setRoleDesc(sysRole.getRoleDesc());
			sysRoleService.save(role);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		} 
		return resMap;
	}
	@RequestMapping(value = "/roleDelete", method = RequestMethod.GET)
	public Map<String,Object> roleDelete(HttpServletRequest request,HttpServletResponse response) {
		String roleId = request.getParameter("roleId");
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(StringHelper.isNotBlack(roleId)){
				SysRole sysRole = sysRoleService.findByKey(roleId);
				sysRole.setRoleState("0");
				sysRoleService.save(sysRole);
			}
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	
}
